package com.fit.pages.manufacturer;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.PropertyAccess;
import org.apache.tapestry5.util.EnumSelectModel;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.UserAccess;
import com.fit.dao.manufacturer.ManufacturerDAO;
import com.fit.dao.user.UserDAO;
import com.fit.dao.wholesale.WholesaleDAO;
import com.fit.entities.Manufacturer;
import com.fit.entities.User;
import com.fit.entities.Wholesale;
import com.fit.model.glov.GenericSelectModel;
import com.fit.model.type.UserType;
import com.fit.model.user.MD5;

@AdminAccess
public class InsertManufacturer {

	@Property
	@SessionState(create = false)
	private Manufacturer manufacturer;
	
	@Inject
	private ManufacturerDAO manufacturerDAO;
	
	@Inject
	private Messages messages;

	@Inject
	private PropertyAccess access;

	@Property
	private Wholesale wholesale;

	@Inject
	private WholesaleDAO wholesaleDAO;

	@Property
	@Persist
	private UserType userType;
	
	@Component
	private Form createForm;

	@Persist
	@Property
	private String error;
	
	@Persist
	@Property
	private String confirmation;
	

	private GenericSelectModel<Wholesale> wholesales; // lista koja ce da se
													// prikaza u cb

	void onPrepare() {
		manufacturer = new Manufacturer();
	}

	// geter za kolekciju koja se prikazuje

	public GenericSelectModel<Wholesale> getWholesales() { 
		wholesales = new GenericSelectModel<Wholesale>(wholesaleDAO.findAll(),
				Wholesale.class, "name", "name", access);
		return wholesales;
	}

//	public SelectModel getTypes() {
//		return new EnumSelectModel(UserType.class, messages);
//	}

	
	Object onSubmitFromCreateForm() {
		error="";
		confirmation="";
		Manufacturer temp = manufacturerDAO.findById(manufacturer.getId());
		if (temp == null) {
//			if (userType == null) {
//				error = "You didn't choose type!";
//				return null;
//			}
//			user.setType(userType.toString());
			manufacturer.setWholesale(wholesale);
			if(manufacturer.getWholesale()==null) {

				String s = new String();
				manufacturer.setWholesale(wholesaleDAO.findByName(s));
			}
//			user.setPassword(MD5.md5(user.getPassword()));
			if (manufacturerDAO.save(manufacturer)) {
				confirmation = "User successfully added!";
				return null;
			}
			confirmation = "User is null!";
			return null;
		}
//		userType=null;
		error = "Company \"" + manufacturer.getCompany()
				+ "\" already exists!";
		return null;
	}
	
	@OnEvent(component = "ResetConfLink")
	Object onResetConfLink() {
		confirmation="";
		return null;
	}
	
	@OnEvent(component = "ResetErrorLink")
	Object onResetErrorLink() {
		error="";
		return null;
	}
	
	
	public boolean isErrorNotNull(){
		if(error==null || error==""){
			return false;
		}
		return true;
	}
	
	public boolean isConfirmationNotNull(){
		if(confirmation==null || confirmation==""){
			return false;
		}
		return true;
	}

}
